﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.Windows.Controls;

namespace MedicalInterface.ViewModel
{
    public class HomeViewModel : BaseViewModel
    {
        #region variables

        private ObservableCollection<UserServiceReference.User> _listUser;
        private ObservableCollection<PatientServiceReference.Patient> _listPatient;

        private UserServiceReference.ServiceUserClient serviceUserClient;
        private PatientServiceReference.ServicePatientClient servicePatientClient;
        private ObservationServiceReference.ServiceObservationClient serviceObservationClient;

        private bool _right;
        #endregion
     
        private bool CanCommandExecute(object parameter)
        {
            return _right;
        }

        #region command
        private ICommand _addUserCommand;
        private ICommand _remUserCommand;
        private ICommand _remPatientCommand;
        private ICommand _addPatientCommand;
        private ICommand _addObsCommand;
        #endregion

        #region getter / setter

        /// <summary>
        /// Get the patient list
        /// </summary>
        public ObservableCollection<PatientServiceReference.Patient> ListPatient
        {
            get { return _listPatient; }
            set { _listPatient = value; }
        }

        /// <summary>
        /// command pour ajouter une personne
        /// </summary>
        public ICommand AddUserCommand
        {
            get
            {
                if (_addUserCommand == null)
                {
                    _addUserCommand = new RelayCommand(NewUser, CanCommandExecute);
                }
                return _addUserCommand;
            }
            set { _addUserCommand = value; }
        }


        /// <summary>
        /// commande pour ajouter une observation
        /// </summary>
        public ICommand AddObsCommand
        {
            get
            {
                if (_addObsCommand == null)
                {
                    _addObsCommand = new RelayCommand(NewObs, CanCommandExecute);
                }
                return _addObsCommand;
            }
            set { _addObsCommand = value; }
        }

        /// <summary>
        /// commande pour supprimer un utilisateur
        /// </summary>
        public ICommand RemUserCommand
        {
            get
            {
                if (_remUserCommand == null)
                {
                    _remUserCommand = new RelayCommand(RemUser, CanCommandExecute);
                }
                return _remUserCommand;
            }
            set { _remUserCommand = value; }
        }

        /// <summary>
        /// commande pour ajouter un patient
        /// </summary>
        public ICommand AddPatientCommand
        {
            get
            {
                if (_addPatientCommand == null)
                {
                    _addPatientCommand = new RelayCommand(NewPatient, CanCommandExecute);
                }
                return _addPatientCommand;
            }
            set { _addPatientCommand = value; }
        }

        /// <summary>
        /// commande pour supprimer un patient
        /// </summary>
        public ICommand RemPatientCommand
        {
            get
            {
                if (_remPatientCommand == null)
                {
                    _remPatientCommand = new RelayCommand(RemPatient, CanCommandExecute);
                }
                return _remPatientCommand;
            }
            set { _remPatientCommand = value; }
        }

        /// <summary>
        /// contient la liste des utilisateurs
        /// </summary>
        public ObservableCollection<UserServiceReference.User> ListUser
        {
            get { return _listUser; }
            set
            {
                if (_listUser != value)
                {
                    _listUser = value;
                    OnPropertyChanged("ListUser");
                }
            }
        }

        #endregion

        /// <summary>
        /// constructeur
        /// </summary>
        public HomeViewModel(bool right)
        {
            // Init
            _right = right;

            try
            {
                serviceUserClient = new UserServiceReference.ServiceUserClient();
                servicePatientClient = new PatientServiceReference.ServicePatientClient();
                serviceObservationClient = new ObservationServiceReference.ServiceObservationClient();
                ListUser = new ObservableCollection<UserServiceReference.User>(serviceUserClient.GetListUser());
                ListPatient = new ObservableCollection<PatientServiceReference.Patient>(servicePatientClient.GetListPatient());
            }
            catch { }
        }

        #region fonctions
        /// <summary>
        /// action permettant d'ajouter une personne à la liste
        /// </summary>
        private void NewUser(object parameter)
        {
            View.AddUserView window = new View.AddUserView();
            ViewModel.AddUserViewModel vm = new AddUserViewModel(ref _listUser);
            
            window.DataContext = vm;
            window.Show();
        }

        /// <summary>
        /// action permettant d'ajouter une observation à l'utilisateur
        /// </summary>
        private void NewObs(object parameter)
        {
            MedicalInterface.PatientServiceReference.Patient tmp;
            Nullable<bool> result = false;


            if (parameter != null)
            {
                tmp = (MedicalInterface.PatientServiceReference.Patient)parameter;

                View.AddObsView window = new View.AddObsView();
                ViewModel.AddObsViewModel vm = new ViewModel.AddObsViewModel(ref tmp);

                window.DataContext = vm;
                result = window.ShowDialog();
                if (result == true)
                {
                    servicePatientClient.DeletePatient(tmp.Id);
                    ListPatient.Remove(tmp);
                }
            }
        }

        /// <summary>
        /// action permettant d'ajouter un patient
        /// </summary>
        private void NewPatient(object parameter)
        {
            View.AddPatientView window = new View.AddPatientView();
            ViewModel.AddPatientViewModel vm = new ViewModel.AddPatientViewModel(ref _listPatient);

            window.DataContext = vm;
            window.Show();
        }

        /// <summary>
        /// action permettant de supprimer un patient
        /// </summary>
        private void RemPatient(object parameter)
        {
            MedicalInterface.PatientServiceReference.Patient tmp;
            Nullable<bool> result = false;


            if (parameter != null)
            {
                tmp = (MedicalInterface.PatientServiceReference.Patient)parameter;

                View.RemPatientView window = new View.RemPatientView();
                ViewModel.RemPatientViewModel vm = new RemPatientViewModel(tmp.Name);

                window.DataContext = vm;
                result = window.ShowDialog();
                if (result == true)
                {
                    servicePatientClient.DeletePatient(tmp.Id);
                    ListPatient.Remove(tmp);
                }
            }
        }

        /// <summary>
        /// action permettant de supprimer un utilisateur
        /// </summary>
        private void RemUser(object parameter)
        {
            MedicalInterface.UserServiceReference.User tmp;
            Nullable<bool> result = false;


            if (parameter != null)
            {
                tmp = (MedicalInterface.UserServiceReference.User)parameter;
            
                View.RemUserView window = new View.RemUserView();
                ViewModel.RemUserViewModel vm = new RemUserViewModel(tmp.Login);

                window.DataContext = vm;
                result = window.ShowDialog();
                if (result == true)
                {
                    try
                    {
                        serviceUserClient.DeleteUser(tmp.Login);
                        ListUser = new ObservableCollection<UserServiceReference.User>(serviceUserClient.GetListUser());
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            //(serviceUserClient.AddUser)
        }
        #endregion
    }
}
